package edu.lingnan.rili.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import edu.lingnan.rili.pojo.Match;
import edu.lingnan.rili.service.MatchService;
import edu.lingnan.rili.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import edu.lingnan.rili.service.MatchService;
import edu.lingnan.rili.utils.Result;
import edu.lingnan.rili.vo.OtherBonusPointsViewPages;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.List;

import static edu.lingnan.rili.controller.AdministratorController.isAdminLogin;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 陈世杰
 * @since 2022-01-23
 */
@RestController
@RequestMapping("/match")
@Api(tags = "学生比赛信息控制器")
public class MatchController {
    @Autowired
    private MatchService service;

    @PostMapping("/save")
    @ApiOperation("新增学生比赛信息")
    public Result<Match> save(@ApiParam(value = "学生比赛信息对象") @RequestBody Match match,
                              @ApiIgnore HttpSession session) {
        // 判断是否登录或者登录过期
        if (isAdminLogin(session)) {
            return new Result<>(203, "还未登录或登录过期，请重新登录");
        }
        Result<Match> matchResult = new Result<>();
        if (service.save(match)) {
            matchResult.setResultCode(200);
            matchResult.setMessage("添加成功");
            matchResult.setData(match);
            return matchResult;
        } else {
            return new Result<>(201, "添加错误");
        }
    }

    @PostMapping("/login")
    @ApiOperation("学生登录功能")
    public Result<Match> login(@ApiParam(value = "学生比赛信息对象") @RequestBody Match match, @ApiIgnore HttpSession session) {
        // 密码进行md5加密
        String password = match.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        LambdaQueryWrapper<Match> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Match::getUsersId, match.getUsersId());
        Match matchInfo = service.getOne(wrapper);

        // 账号不存在
        if (matchInfo == null) {
            return new Result<>(201, "该账号不存在，请检查账号是否正确");
        }

        // 密码输入有误
        if (!password.equals(matchInfo.getPassword())) {
            return new Result<>(201, "密码输入错误，请检查是否输入正确");
        }

        // 登录成功，将学号存入Session并返回登录成功结果
        session.setAttribute("match", matchInfo);
        Result<Match> result = new Result<>();
        result.setResultCode(200);
        result.setMessage("登录成功");
        result.setData(matchInfo);
        return result;
    }
}

